package com.hfzy.ihk.web.aliyuncall.utils;

import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.facade.aliyuncall.vo.CallHistory;
import com.hfzy.ihk.web.aliyuncall.constant.RedisFields;
import com.hfzy.ihk.web.aliyuncall.constant.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by Administrator on 2018/7/23.
 *
 * @author wws
 */
@Component
public class CallHistoryUtils {

    private static RedisTemplate<String,String> redisTemplate;

    @Autowired
    public static void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        CallHistoryUtils.redisTemplate = redisTemplate;
    }

    public static CallHistory fromMap(Map<String,String> callHistoryMap){
        CallHistory callHistory = new CallHistory();
        try {

            for(String field :callHistoryMap.keySet()){

                String value = callHistoryMap.get(field);

                switch (field){
                    case RedisFields.CREATE_USER_ID:
                        callHistory.setCreateUserId(value);
                        break;
                    case RedisFields.CREATE_TIME:
                        callHistory.setCreateTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        callHistory.setToday(Utils.date2DateString(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS)));
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_PHONE :// "mainCallPhone"; //主叫；由拨号时发送过来
                        callHistory.setMainCallPhone(value);
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_PHONE :// "targetCallPhone"; //被叫；由拨号时发送过来
                        callHistory.setTargetCallPhone(value);
                        break;
                    case RedisFields.CALL_HISTORY_SHOW_NUM :// "showNum"; //主显号码：小号、E1固话等；由拨号时发送过来
                        callHistory.setShowNum(value);
                        break;
                    case RedisFields.CALL_HISTORY_IMS :// "ims";
                        callHistory.setIms(value);
                        break;
                    case RedisFields.CALL_HISTORY_DATA_ID :// "dataId"; //置业或app数据库 数据id，用于jms推送数据时，置业或app更新数据的索引；这要求置业或app保存数据成功后再提交呼叫任务到云呼
                        callHistory.setDataId(value);
                        break;
                    case RedisFields.CALL_HISTORY_WHERE_FROM :// "whereFrom"; //来源：app,置业
                        callHistory.setWhereFrom(Integer.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_PAGE_FROM :// "pageFrom"; //app盘源,app联动call,置业盘源，置业联动call? （websocket）
                        callHistory.setPageFrom(Integer.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_MACHINE_FROM :// "machineFrom";//机器码：用于定位websocket对象（websocket）
                        callHistory.setMachineFrom(value);
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_TYPE :// "mainCallType"; //谁呼出？：经纪人/客户
                        callHistory.setMainCallType(Integer.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_CUSTOMER_NAME :// "customerName"; //客户名称
                        callHistory.setCustomerName(value);
                        break;
                    case RedisFields.CALL_HISTORY_PROJECT :// "project"; //项目名
                        callHistory.setProject(value);
                        break;
                    case RedisFields.CALL_HISTORY_PROPERTY_NO :
                        callHistory.setPropertyNo(value);
                        break;
                    case RedisFields.CALL_HISTORY_OBJECT_ID :
                        callHistory.setObjectId(value);
                        break;
                    case RedisFields.CALL_HISTORY_COMMIT_TO :// "commitTo"; //提交到哪里：东进/电信
                        callHistory.setCommitTo(Integer.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_COMMIT_OK :// "commitOk"; //提交呼叫任务到CTI/福建电信 成功
                        callHistory.setCommitOk(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_COMMIT_TIME :// "提交时间
                        callHistory.setCommitTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;

                    case RedisFields.CALL_HISTORY_CITY:
                        callHistory.setCity(Integer.valueOf(value));    // "city"; //经纪所在的城市
                        break;
                    case RedisFields.CALL_HISTORY_SESSION_ID :// "sessionId"; //会话ID
                        callHistory.setSessionId(value);
                        break;
                    case RedisFields.CALL_HISTORY_CALL_TYPE :// "callType"; //通话类型：呼出、呼入
                        callHistory.setCallType(Integer.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_RECORD_FILE_URL :// "recordFileUrl"; //录音文件地址
                        callHistory.setRecordFileUrl(value);
                        break;
                    case RedisFields.CALL_HISTORY_RECORD_TIME_LENTH :// "recordTimeLength"; //录音时长（S）
                        callHistory.setRecordTimeLength(Long.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_RECORD_FILE_URL_PART_A:   //recordFileUrl //语音分析录音文件地址
                        callHistory.setRecordFileUrlPartA(value);
                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_TYPE:           // voiceType     //语音分析结果
//                        callHistory.setVoiceType(value);
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_CATEGORY:       // voiceCategory //语音分析结果分类
//                        callHistory.setVoiceCategory(value);
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_CONTENT:        // voiceContent  //语音内容
//                        callHistory.setVoiceContent(value);
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_STATUS:
//                        callHistory.setVoiceStatus(Integer.valueOf(value));
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_ENGINE:
//                        callHistory.setVoiceEngine(Integer.valueOf(value));
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_START_TIME:
//                        callHistory.setVoiceStartTime(DateUtils.getDateFromString(value, DateUtils.DATE_FORMAT_DATETIME_SSS));
//                        break;
//                    case RedisFields.CALL_HISTORY_VOICE_END_TIME:
//                        callHistory.setVoiceEndTime(DateUtils.getDateFromString(value, DateUtils.DATE_FORMAT_DATETIME_SSS));
//                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_RINGING :// "mainCallRinging"; //主叫响铃
                        callHistory.setMainCallRinging(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_RINGING_TIME :// "mainCallRingingTime"; //主叫响铃时间
                        callHistory.setMainCallRingingTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_MAIN_CALL_RINGING_TIME :// "receiveMainCallRingingTime"; //接收主叫响铃状态时间
                        callHistory.setReceiveMainCallRingingTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_ANSWERED :// "mainCallAnswered"; //主叫应答
                        callHistory.setMainCallAnswered(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_ANSWERED_TIME :// "mainCallAnsweredTime"; //主叫应答
                        callHistory.setMainCallAnsweredTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_HISTORY_MAIN_CALL_ANSWERED_TIME :// "receiveMainCallAnsweredTime"; //接收主叫应答状态时间
                        callHistory.setReceiveMainCallAnsweredTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;

                    case RedisFields.CALL_HISTORY_MAIN_CALL_FAILED :// "mainCallFailed"; //呼叫主叫失败：如果没有响铃，表示主叫空号；如有响铃，表示未接听或把来电号码加入黑名单或手机飞行模式（电信）
                        callHistory.setMainCallFailed(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_MAIN_CALL_FAILED_TIME :// "mainCallFailedTime"; //呼叫主叫失败时间
                        callHistory.setMainCallFailedTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_MAIN_CALL_FAILED_TIME :// "receiveMainCallFailedTime"; //接收呼叫主叫失败状态时间
                        callHistory.setReceiveMainCallFailedTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_RINGING :// "targetCallRinging"; //被叫响铃
                        callHistory.setTargetCallRinging(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_RINGING_TIME :// "targetCallRingingTime"; //被叫响铃时间
                        callHistory.setTargetCallRingingTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_TARGET_CALL_RINGING_TIME :// "receiveTargetCallRingingTime"; //接收被叫响铃状态时间
                        callHistory.setReceiveTargetCallRingingTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;

                    case RedisFields.CALL_HISTORY_TARGET_CALL_ANSWERED :// "targetCallAnswered"; //被叫应答
                        callHistory.setTargetCallAnswered(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_ANSWERED_TIME :// "targetCallAnsweredTime"; //被叫应答
                        callHistory.setTargetCallAnsweredTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_TARGET_CALL_ANSWERED_TIME :// "receiveTargetCallAnsweredTime"; //接收被叫应答状态时间
                        callHistory.setReceiveTargetCallAnsweredTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_FAILED :// "targetCallFailed"; //呼叫被叫失败：如果没有响铃，表示被叫空号；如有响铃，表示未接听或把来电号码加入黑名单或手机飞行模式；（电信）
                        callHistory.setTargetCallFailed(Boolean.valueOf(value));
                        break;
                    case RedisFields.CALL_HISTORY_TARGET_CALL_FAILED_TIME :// "targetCallFailedTime"; //呼叫被叫失败时间
                        callHistory.setTargetCallFailedTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_TARGET_CALL_FAILED_TIME :// "receiveTargetCallFailedTime"; //接收呼叫被叫失败状态时间
                        callHistory.setReceiveTargetCallFailedTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.CALL_HISTORY_DISCONNECT_TIME :// "disconnectTime"; //通话结束时间:有此时间表明是正常通话，记录第一次返回disconnect状态的时间
                        callHistory.setDisconnectTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.RECEIVE_CALL_HISTORY_DISCONNECT_TIME :// "receiveDisconnectTime"; //接受通话结束状态时间
                        callHistory.setReceiveDisconnectTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
//                    case RedisFields.RECEIVE_CALL_HISTORY_APP_START_TIME :// "receiveAppStartTime"; //接受APP开始通话时间
//                        callHistory.setReceiveAppStartTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
//                        break;
//                    case RedisFields.RECEIVE_CALL_HISTORY_APP_STOP_TIME :// "receiveAppStopTime"; //接受通话结束状态时间
//                        callHistory.setReceiveAppStopTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
//                        break;
//                    case RedisFields.RECEIVE_CALL_HISTORY_APP_RECORD_FILE_TIME :// "receiveAppRecordFileTime"; //接受app上传文件时间束状态时间
//                        callHistory.setReceiveApprecordFileTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
//                        break;
                    case RedisFields.CALL_HISTORY_MESSAGE_ID :// "messageId"; //jms msg Id
                        callHistory.setMessageId(value);
                        break;
                    case RedisFields.CALL_HISTORY_REMARK :
                        callHistory.setRemark(value);
                        break;
                    case RedisFields.CALL_HISTORY_FAILED_REASON :
                        callHistory.setFailedReason(value);
                        break;
//                    case RedisFields.CALL_HISTORY_POINTS :
//                        callHistory.setPoints(Integer.valueOf(value));
//                        break;
//                    case RedisFields.CALL_HISTORY_MONEY :
//                        callHistory.setMoney(Float.valueOf(value));
//                        break;
                    case RedisFields.INTERRUPT :
                        callHistory.setInterrupt(Integer.valueOf(value));
                        break;
                    case RedisFields.INTERRUPTTIME :
                        callHistory.setInterruptTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.PUBLIC_PUSH_INSERT_QUEUE:
                        callHistory.setIsInserted(Boolean.valueOf(value));
                        break;
                    case RedisFields.MAIN_CALL_START_TIME :
                        callHistory.setMainCallStartTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.MAIN_CALL_END_TIME :
                        callHistory.setMainCallEndTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.MAIN_CALL_LENGTH :
                        callHistory.setMainCallLength(Integer.valueOf(value));
                        break;
                    case RedisFields.TARGET_CALL_START_TIME:
                        callHistory.setTargetCallStartTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.TARGET_CALL_END_TIME :
                        callHistory.setTargetCallEndTime(DateUtils.getDateFromString(value,DateUtils.DATE_FORMAT_DATETIME_SSS));
                        break;
                    case RedisFields.CALL_OUT_RESULT_MESSAGE :
                        callHistory.setCallOutResultMessage(value);
                        break;
                    case RedisFields.CALL_OUTR_ESULT_CODE :
                        callHistory.setCallOutResultCode(value);
                        break;

                }
            }

            /*计算通话时长（ms）*/
            if((callHistory.getCommitTo() == 0 || callHistory.getCallType() == 1)
                    && (callHistory.getTargetCallAnsweredTime()!=null) && (callHistory.getDisconnectTime()!=null)){
                long connectedLength = (callHistory.getDisconnectTime().getTime() - callHistory.getTargetCallAnsweredTime().getTime()) / 1000;
                callHistory.setRecordTimeLength(connectedLength > 0 ? connectedLength : 0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            for(String b:callHistoryMap.keySet()){
                System.out.println("new callHistory failed from RedisHashMap--> key:"+ b+" value:"+callHistoryMap.get(b));
            }
        }finally {
            return callHistory;
        }

    }

    public static Map<String,String> getCallHistoryMapFormRedis(String sessionId){

        Map callHistoryMap = redisTemplate.opsForHash().entries((RedisKey.CALL_HISTORY_RPIX+sessionId));

        if (callHistoryMap.isEmpty() || callHistoryMap == null) {
            return null;
        }

        return callHistoryMap;
    }

    public static CallHistory fromRedisHashMap(String sessionId){

        Map<String,String> callHistoryMap = null ;

        callHistoryMap = getCallHistoryMapFormRedis(sessionId);

        if (callHistoryMap.isEmpty() || callHistoryMap == null) {
            return null;
        }

        return fromMap(callHistoryMap);
    }
}
